cmake_minimum_required (VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
project(DGGSViewer)
if (MSVC)
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
    add_compile_options(/wd4251)
endif ()

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/debug)
else ()
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/release)
endif ()


set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/DGGSViewer)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/DGGSViewer)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/DGGSViewer)

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSBase")

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSKernel")
link_directories("${DGGS_OUT_DIR}/LibDGGSKernel")

include_directories("${CMAKE_SOURCE_DIR}/3rdlibs/include")
link_directories("${CMAKE_SOURCE_DIR}/3rdlibs/lib")

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSFeature")
link_directories("${DGGS_OUT_DIR}/LibDGGSFeature")

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSField")

include_directories("${CMAKE_SOURCE_DIR}/LibGridTIFF")
link_directories("${DGGS_OUT_DIR}/LibGridTIFF")


if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(LIBDK LibDGGSKernelD)
    set(LIBDG LibGridTIFFD)
    set(LIBDF LibDGGSFeatureD)
else ()
    set(LIBDK LibDGGSKernel)
    set(LIBDG LibGridTIFF)
    set(LIBDF LibDGGSFeature)
endif ()



add_executable(DGGSViewer
        main.cpp
        Application.cpp
        DGGSVisualizer.cpp
        Earth.cpp
        FileHandler.cpp
        InputHandler.cpp
)

target_link_libraries(DGGSViewer PRIVATE
        ${LIBDK}
        ${LIBDG}
        ${LIBDF}
        SDL2
        SDL2main
        SDL2_image
        SDL2_ttf
        glew32
        opengl32
)

add_dependencies(DGGSViewer LibDGGSKernel LibDGGSField LibDGGSFeature LibGridTIFF)


if(ISEXTRACT)
    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_custom_command(TARGET DGGSViewer POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/DGGSViewer.exe
                ${lib_output_directory}/bin/debug/DGGSViewer.exe
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_SOURCE_DIR}/DGGSViewer/textures/earth.jpg
                ${lib_output_directory}/bin/debug/earth.jpg
                COMMENT "Copying DGGSViewer debug files..."
        )
    else ()
        add_custom_command(TARGET DGGSViewer POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/DGGSViewer.exe
                ${lib_output_directory}/bin/release/DGGSViewer.exe
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_SOURCE_DIR}/DGGSViewer/textures/earth.jpg
                ${lib_output_directory}/bin/release/earth.jpg
                COMMENT "Copying DGGSViewer release files..."
        )
    endif ()
endif()

add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
        ${CMAKE_SOURCE_DIR}/DGGSViewer/textures/earth.jpg
        ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/earth.jpg)


# LibDGGSBase复制
file(GLOB_RECURSE MyDLL_FILES ${CMAKE_SOURCE_DIR}/3rdlibs/bin/*.dll)
foreach(dll_file ${MyDLL_FILES})
    get_filename_component(dll_dir ${dll_file} DIRECTORY)
    string(REPLACE ${CMAKE_SOURCE_DIR}/3rdlibs/bin ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} output_dll_dir ${dll_dir})
    file(MAKE_DIRECTORY ${output_dll_dir})
    configure_file(${dll_file} ${output_dll_dir} COPYONLY)
endforeach()


if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
            ${DGGS_OUT_DIR}/LibDGGSKernel/LibDGGSKernelD.dll
            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LibDGGSKernelD.dll)

    add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
            ${DGGS_OUT_DIR}/LibDGGSFeature/LibDGGSFeatureD.dll
            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LibDGGSFeatureD.dll)

    add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
            ${DGGS_OUT_DIR}/LibGridTIFF/LibGridTIFFD.dll
            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LibGridTIFFD.dll)

else ()
    add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
            ${DGGS_OUT_DIR}/LibDGGSKernel/LibDGGSKernel.dll
            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LibDGGSKernel.dll)

    add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
            ${DGGS_OUT_DIR}/LibDGGSFeature/LibDGGSFeature.dll
            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LibDGGSFeature.dll)

    add_custom_command(TARGET DGGSViewer POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
            ${DGGS_OUT_DIR}/LibGridTIFF/LibGridTIFF.dll
            ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/LibGridTIFF.dll)
endif ()